package com.ds.robot.assistant.controller.api;

import cn.hutool.core.lang.Dict;
import com.alibaba.fastjson.JSON;
import com.ds.robot.assistant.common.R;
import com.ds.robot.assistant.dto.BugConditionDTO;
import com.ds.robot.assistant.service.BugInfoService;
import com.ds.robot.assistant.vo.BugVO;
import com.ds.robot.assistant.vo.TesterBugVO;
import com.ds.robot.assistant.vo.req.BugCondition;
import com.ds.robot.assistant.vo.resp.ChartData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 作用：
 *
 * @author WeiShaoying
 * 
 */
@RestController(value = "apiBugController")
@RequestMapping("/api/bug")
@Slf4j
public class BugController {

    @Resource
    private BugInfoService bugInfoService;

    private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    private static final int SIZE = 10;

    @PostMapping("/queryBugList")
    public R queryBugList(@RequestBody BugCondition condition) {
        log.info("查询缺陷信息，参数：{}", JSON.toJSONString(condition));
        BugConditionDTO conditionDTO = convert(condition);
        List<BugVO> voList = bugInfoService.selectBugInfoByCondition(conditionDTO);
        return R.ok().put("data", voList);
    }

    private BugConditionDTO convert(BugCondition condition) {
        BugConditionDTO conditionDTO = new BugConditionDTO();
        if (condition != null) {
            conditionDTO.setAccountId(condition.getAccountId());

            //TODO 增加全局异常处理器，参数校验
            if (StringUtils.hasText(condition.getStartDate()) && condition.getStartDate().length() >= SIZE) {
                LocalDate localDate = LocalDate.parse(condition.getStartDate().substring(0, SIZE), formatter);
                LocalDateTime startLocalDateTime = localDate.atStartOfDay();
                long startEpochMilli = startLocalDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
                conditionDTO.setStartTime(startEpochMilli);
            }
            if (StringUtils.hasText(condition.getEndDate()) && condition.getEndDate().length() >= SIZE) {
                LocalDate localDate = LocalDate.parse(condition.getEndDate().substring(0, SIZE), formatter);
                LocalDateTime endLocalDateTime = localDate.atTime(23, 59, 59, 999_999_999);
                long endEpochMilli = endLocalDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
                conditionDTO.setEndTime(endEpochMilli);
            }
        }
        return conditionDTO;
    }

    @PostMapping("/getLineChartData1")
    public R getLineChartData4NewBug(@RequestBody BugCondition condition) {
        log.info("getLineChartData4NewBug，参数：{}", condition);
        BugConditionDTO conditionDTO = convert(condition);
        List<ChartData> dataList = bugInfoService.selectChartDataByCondition(conditionDTO);
        return R.ok().put("data", dataList);
    }

    @PostMapping("/getLineChartData")
    public R getLineChartData(@RequestBody BugCondition condition) {
        log.info("getLineChartData，参数：{}", condition);
        BugConditionDTO conditionDTO = convert(condition);
        Dict dict = bugInfoService.selectLineChartDataByCondition(conditionDTO);
        return R.ok().put("data", dict);
    }


    @PostMapping("/queryTesterBugList")
    public R queryTesterBugList(@RequestBody BugCondition condition) {
        log.info("查询测试缺陷列表，参数：{}", JSON.toJSONString(condition));
        BugConditionDTO conditionDTO = convert(condition);
        List<TesterBugVO> voList = bugInfoService.selectTesterBugByCondition(conditionDTO);
        return R.ok().put("data", voList);
    }

    @PostMapping("/getTesterLineChartData")
    public R getTesterLineChartData(@RequestBody BugCondition condition) {
        log.info("getTesterLineChartData，参数：{}", condition);
        BugConditionDTO conditionDTO = convert(condition);
        Dict dict = bugInfoService.selectTesterLineChartDataByCondition(conditionDTO);
        return R.ok().put("data", dict);
    }

}
